from fastapi import APIRouter, UploadFile, File, Depends
from sqlalchemy.orm import Session

from controller.FileAdminController import FileAdminController
from controller.PublicAdminController import PublicAdminController
from util.Auth import check_token
from util.DataBaseTools import get_db_session
from util.ErrorCode import ErrorCode
from util.LogHelper import Log

file_admin_router = APIRouter()


@file_admin_router.post("/upload/file", summary="文件上传")
def upload_single_file(path: str, file: UploadFile = File(...), db: Session = Depends(get_db_session), result=Depends(check_token)):
    Log.getlogger().debug(f'recieved request:/upload/file')
    if result.get('code') == 0:
        if PublicAdminController.has_permission(result.get('data').port_id, '/employment/add', db):
            response = FileAdminController.upload_file(path, file)
            return response
        else:
            return {'code': ErrorCode.USERID_HAS_NO_PERMISSION.value, 'message': 'user has no permission'}
    else:
        return result
